package tt.dz.egobus.api.impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.StringUtils;
import tt.dz.egobus.annotation.LogAuto;
import tt.dz.egobus.api.ResponseBack;
import tt.dz.egobus.api.common.CommonApiService;
import tt.dz.egobus.api.exception.ResponseException;
import tt.dz.egobus.api.managerservice.bustype.BusTypeApiService;
import tt.dz.egobus.api.managerservice.bustype.BusTypeVo;
import tt.dz.egobus.api.util.ResponseHelper;
import tt.dz.egobus.bean.CompareOpEnum;
import tt.dz.egobus.servicemanager.entities.BusType;
import tt.dz.egobus.servicemanager.service.BusTypeService;
import tt.dz.egobus.type.LogType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static tt.dz.egobus.api.util.ResponseHelper.copy;
import static tt.dz.egobus.bean.I18NCodeEnum.RPC_PARAM_ILLEGAL_ERROR;

/**
 * @author Anson
 * @date 17-3-15
 * @name 车型公共服务实现类
 * @since 1.0.1
 */
@Service(version = "1.0.1", interfaceClass = BusTypeApiService.class)
public class BusTypeApiServiceImpl extends CommonApiService implements BusTypeApiService {

    private static final String BUS_TYPE_SERVICE_NAME = "busTypeServiceImpl";


    @LogAuto(bizCode =20090001,bizDesc = "查询车辆详细信息",bizType = LogType.DUBBO)
    @Override
    public ResponseBack<BusTypeVo> queryInfo(String id) {
        if (StringUtils.isEmpty(id)) {
            return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
        }
        BusTypeService busTypeService = this.getService(BUS_TYPE_SERVICE_NAME, BusTypeService.class);
        BusType busType = busTypeService.get(id);
        if (busTypeService==null) {
            return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
        }
        BusTypeVo busTypeVo = copy(busType, new BusTypeVo());
        return ResponseBack.success(busTypeVo);
    }

    @Override
    public ResponseBack<List<BusTypeVo>> queryInfos(String[] ids) {
        if (null == ids || ids.length == 0) {
            return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
        }
        BusTypeService busTypeService = this.getService(BUS_TYPE_SERVICE_NAME, BusTypeService.class);
        List<BusType> busTypes = busTypeService.queryByIds(ids);
        List<BusTypeVo> busTypeVo = ResponseHelper.copy(busTypes, BusTypeVo.class);
        return ResponseBack.success(busTypeVo);
    }

    @Override
    public ResponseBack<List<BusTypeVo>> queryAll() throws ResponseException {
        BusTypeService busTypeService = this.getService(BUS_TYPE_SERVICE_NAME, BusTypeService.class);
        List<BusType> busTypes = busTypeService.list(null);
        List<BusTypeVo> busTypeVo = ResponseHelper.copy(busTypes, BusTypeVo.class);
        return ResponseBack.success(busTypeVo);
    }

    @Override
    public ResponseBack<List<BusTypeVo>> queryBySeatNum(int seatNum, CompareOpEnum op) throws ResponseException {
        Map<String, Object> params = new HashMap<String, Object>();
        boolean isValidate = op==null;
        if (isValidate) {
            throw new ResponseException(I18N_PROPERTIES.get(String.valueOf(RPC_PARAM_ILLEGAL_ERROR)));
        }
        params.put(op.getMsg() + "#seatNum", seatNum);
        BusTypeService busTypeService = this.getService(BUS_TYPE_SERVICE_NAME, BusTypeService.class);
        List<BusType> busTypes = busTypeService.list(params);
        List<BusTypeVo> busTypeVo = ResponseHelper.copy(busTypes, BusTypeVo.class);
        return ResponseBack.success(busTypeVo);
    }


}
